# https://www.cnblogs.com/maplethefox/p/10989201.html
# 自定义线程类
# 1.创建步骤
# (1)继承Thread类
# (2)重写__init__方法添加自己的属性，使用super加载父类属性
# (3)重写run方法
# 2.使用方法
# (1)实例化对象
# (2)调用start自动执行run方法
# (3)调用join回收线程
from threading import Thread
from time import sleep, ctime


class MyThread(Thread):
    def __init__(self, target=None, args=(), kwargs={}):
        super().__init__()
        self.target = target
        self.args = args
        self.kwargs = kwargs

    def run(self):
        self.target(*self.args, **self.kwargs)


# 通过完成上面的MyThread使程序正常运行，通过下面程序可以让player作为线程运行，player只是测试函数，名称和参数均不确定
def player(sec, song):
    for i in range(3):
        print('Playing-> %s <-: %s' % (song, ctime()))
        sleep(sec)


if __name__ == '__main__':
    t1 = MyThread(target=player, args=(2,), kwargs={'song': 'Outlaws Of Love'})
    t2 = MyThread(target=player, args=(2,), kwargs={'song': 'See you again'})
    t1.start()
    t2.start()
    t1.join()
    t2.join()
